<?php

namespace App\Mobile\Controllers;

use DB;
use Auth;
use Validator;
use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\User;
use App\WxPublicUser;
use App\Room;
use App\Club;
use App\Agent;

class RoomController extends Controller
{
	/**
	 * [index 桌子列表]
	 * @return [type] [description]
	 */
	public function index(Agent $agent)
	{
		$user = $agent->user();
		$club_id = [];
		$normal_club = Club::where(['creator' => $user->userid, 'type' => 1])->first();//普通俱乐部
		if ($normal_club) {
			array_push($club_id, $normal_club->club_id);
		}
		$member_club = Club::where(['creator' => $user->userid])->whereIn('type', [2,3,4,5,6])->first();//会员俱乐部
		if ($member_club) {
			array_push($club_id, $member_club->club_id);
		}
		$rooms = Room::whereIn('club_id', $club_id)->get();
		$count = Room::whereIn('club_id', $club_id)->count();

		return view('mobile.rooms.index', compact('rooms', 'count'));
	}

	/**
	 * [dissRoom 解散房间]
	 * @return [type] [description]
	 */
	public function dissRoom(Request $request)
	{
		if (!$request->input('room_id')) {
			return ['flag' => false, 'msg' => 'room_id不能为空'];
		}
		$room = Room::where(['id' => $request->input('room_id')])->first();
		if (!$room) {
			return ['flag' => false, 'msg' => '房间不存在'];
		}
		$url = 'http://39.106.36.158:9003/remove_club_room?creator='.$room->club->creator.'&club_id='.$room->club_id.'&roomid='.$room->id;
		$curl = curl_init();
		//设置抓取的url
		curl_setopt($curl, CURLOPT_URL, $url);
		//设置获取的信息以文件流的形式返回，而不是直接输出。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//执行命令
		$data = curl_exec($curl);
		//关闭URL请求
		curl_close($curl);
		//显示获得的数据
		$data = json_decode($data, true);
		if ($data['errcode'] == 0) {
			return ['flag' => true];
		} else {
			return ['flag' => false, 'msg' => $data['errmsg']];
		}
	}	

}